import * as types from '../mutation-types';
import {fetch} from 'utils/api.js';
import Tools from 'utils/tools.js';
import querystring from 'querystring';
import loginTools from 'utils/login.js';

const state = {
  role: "",
  status: ""
};

const mutations = {
  // 设置用户信息
  [types.SET_USER_DETAIL](state, detail) {
    state.role = detail.role;
    state.status = detail.status;
  }
};

const actions = {
  loadUserDetail({commit, dispatch, state}) {
    return new Promise((resolve, reject) => {
      fetch('userInfo').then((res) => {
        if (res.code === 200) {      
          commit(types.SET_USER_DETAIL, res.data);
          resolve(res);
        } else if(res.code === 401){
          alert(res.message);
          reject(res);
        }else {
          throw new Error('Response Error');
        }
      });
    }, (res) => {
      reject(res);
    });
  }
};

export default {
  state,
  mutations,
  actions
};
